package com.pyue.eam.regi.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyue.eam.admin.api.entity.regi.CqRegiLibEntity;
import com.pyue.eam.admin.api.entity.regi.CqTaskLibEntity;
import com.pyue.eam.admin.api.entity.regi.CqTaskNgEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.app.service.AppUserService;
import com.pyue.eam.common.data.resolver.ParamResolver;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.mapper.CqTaskLibMapper;
import com.pyue.eam.regi.mapper.CqTaskNgMapper;
import com.pyue.eam.regi.mapper.PartLibMapper;
import com.pyue.eam.regi.service.CqRegiLibService;
import com.pyue.eam.regi.service.CqTaskNgService;
import com.pyue.eam.admin.utils.WxPushMessageUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 不良输入
 *
 * @date 2023-11-17 15:34:17
 */
@Service
@AllArgsConstructor
public class CqTaskNgServiceImpl extends ServiceImpl<CqTaskNgMapper, CqTaskNgEntity> implements CqTaskNgService {
    private final CqTaskLibMapper cqTaskLibMapper;
    private final AppUserService appUserService;
    private final PartLibMapper partLibMapper;
    private final CqRegiLibService cqRegiLibService;

    @Override
    public Boolean saveData(CqTaskNgEntity cqTaskNg) {
        ApiAsserts.isTrue(cqTaskNg.getTaskId() != null, "任务id不能为空");
        CqTaskLibEntity cqTaskLib = cqTaskLibMapper.selectById(cqTaskNg.getTaskId());
        ApiAsserts.isTrue(cqTaskLib != null, "任务不存在");
        ApiAsserts.isTrue(Convert.toInt(cqTaskNg.getFaultyNum(), 0) > 0, "不良数量必须大于0");

        assert cqTaskLib != null;

        cqTaskNg.setUserAvatar(SecurityUtils.getUser().getAvatar());
        boolean save = save(cqTaskNg);

        // 发生不良通知企业和团长
        sandNgSaveNotice(cqTaskLib, save, cqTaskNg);
        return Boolean.TRUE;
    }

    // 发生不良通知企业和团长
    private void sandNgSaveNotice(CqTaskLibEntity cqTaskLib, boolean save, CqTaskNgEntity cqTaskNg) {
        // 团负责人appUserId
        Long regiChargeId = cqTaskLib.getRegiChargeId();
        Long taskCreateBy = Convert.toLong(cqTaskLib.getCreateBy(), 0L);

        // 企业方
        List<Long> userIdList = CollUtil.newArrayList(taskCreateBy);

        // 团长
        if (regiChargeId != null && !Objects.equals(SecurityUtils.getUserId(), regiChargeId)) {
            userIdList.add(regiChargeId);
        }

        // 微信公众号openId
        List<String> wxOpenIdList = appUserService.getWxOpenIdByAppUserId(userIdList);
        // 团信息
        CqRegiLibEntity regiLib = cqRegiLibService.getRegiLibByUserId(cqTaskLib.getRegiId());


        // 发送任务不良通知给企业和团长-如果团长是自己则不需要发送
        if (save && CollUtil.isNotEmpty(wxOpenIdList) && ParamResolver.getBool("MP_NOTICE")) {
            // 质检结果
            StringBuilder result = new StringBuilder();
            result.append("发现").append(cqTaskNg.getFaultyNum()).append("件不良,");
            if (StrUtil.isNotBlank(cqTaskNg.getPartBatch())) {
                result.append("批次号:").append(cqTaskNg.getPartBatch()).append(",");
            }
            if (StrUtil.isNotBlank(cqTaskNg.getReasonDesc())) {
                result.append("失效描述:").append(cqTaskNg.getReasonDesc()).append(",");
            }
            result.append("请及时处理");


            // 发送微信公众号通知-企业
            HashMap<String, String> dataMap = MapUtil.newHashMap();
            dataMap.put("thing7", partLibMapper.getNameById(cqTaskLib.getPartId()));  // 产品名称
            dataMap.put("thing4", regiLib.getRegiName() + "-团长");  // 质检人
            dataMap.put("time5", LocalDate.now().toString());  // 质检时间
            dataMap.put("thing3", result.toString());  // 质检结果
            // 推送消息
            WxPushMessageUtil.templateMessage(wxOpenIdList, "orZIvT5NiA5jnN8byyGndytGIWUDX5an3AmEi5APqsE", dataMap);
        }
    }
}
